﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VelesPresentation;
using VelesDBCore;

namespace VelesUIWinForm
{
    public partial class FormCategory : Form, ICategoryView
    {
        CategoryPresenter presenter;

        List<Category> categories;
        List<VelesDBCore.Attribute> attributes;
        List<VelesDBCore.Attribute> categoryAttributes;

        public List<Category> Categories
        {
            set { categories = value; }
        }

        public List<VelesDBCore.Attribute> Attributes
        {
            set { attributes = value; }
        }

        public List<VelesDBCore.Attribute> CategoryAttributes
        {
            set { categoryAttributes = value; }
        }

        public FormCategory()
        {
            InitializeComponent();
            presenter = new CategoryPresenter(this);
            RefreshCategory();
        }

        private void RefreshCategory()
        {
            if (GetCategories != null) GetCategories();
            FillTreeView();
        }

        private void FillTreeView()
        {
            trvCategory.Nodes.Clear();
            foreach (Category category in categories)
                if (category.ParentCategoryID == null) InsertNode(null, category);
        }

        private void InsertNode(TreeNode node, Category category)
        {
            TreeNode treeNode = new TreeNode();
            treeNode.Tag = category;
            treeNode.Text = category.CategoryName;
            if (node == null) trvCategory.Nodes.Add(treeNode);
            else node.Nodes.Add(treeNode);
            foreach (Category subCategory in category.Category1) InsertNode(treeNode, subCategory);
        }


        #region Члены ICategoryView

       

        public Category CurrentCategory
        {
            get
            {
                if (trvCategory.SelectedNode != null) return (Category)trvCategory.SelectedNode.Tag;
                return null;
            }
        }

        public List<VelesDBCore.Attribute> SelectedAttributes
        {
            get { return (List<VelesDBCore.Attribute>)gridAttribute.SelectedRows.AsQueryable(); }
        }

        public List<VelesDBCore.Attribute> SelectedCategoryAttributes
        {
            get { return (List<VelesDBCore.Attribute>)gridSelectedAttribute.SelectedRows.AsQueryable(); }
        }

        public event VoidEventHandler GetCategories;

        public event VoidEventHandler AddCategory;

        public event VoidEventHandler AddSubCategory;

        public event VoidEventHandler EditCategory;

        public event VoidEventHandler RemoveCategory;

        public event VoidEventHandler AddAttributesToCategory;

        public event VoidEventHandler RemoveAttributesFromCategory;

        #endregion

        #region Члены IView

        public event VoidEventHandler PrepareView;

        #endregion

        private void btnAttributeManage_Click(object sender, EventArgs e)
        {
            FormAttribute formAttribute = new FormAttribute();
            formAttribute.ShowDialog();
        }
    }
}
